package pool;

import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Level;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.*;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import static java.util.logging.Level.FINE;

/**
 * @author: User
 * @date: 2022/11/9
 * @Description:此类用于xxx
 */
public class AllowCoreThread {
    public Logger logger = Logger.getLogger("print");


    @Test
    public void allowCoreThread01() throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 10, TimeUnit.MINUTES, new SynchronousQueue<>(false));
        executor.allowCoreThreadTimeOut(true);
        Future<String> exe = (Future<String>) executor.submit(new PrintInfoRunnable());
        String sre = exe.get();
    }

    @Test
    public void allowCoreThread02() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 10, TimeUnit.MINUTES, new SynchronousQueue<>(false));
        executor.allowCoreThreadTimeOut(true);
        executor.execute(() -> {
            try {
                FileReader fileReader = new FileReader("src/main/resources/test.txt");
                char[] cbuf = new char[100];
                fileReader.read(cbuf, 0, 99);

                LogRecord record = new LogRecord(FINE, String.valueOf(cbuf));
                logger.log(record);
            } catch (FileNotFoundException e) {
                logger.log(FINE, "处理异常");
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }


    @Test
    public void allowCoreThread03() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 10, TimeUnit.MINUTES, new SynchronousQueue<>(false));
        System.out.println(executor.getActiveCount());
        System.out.println(executor.getCorePoolSize());
        System.out.println(executor.getCompletedTaskCount());
        System.out.println(executor.getLargestPoolSize());
        System.out.println(executor.getMaximumPoolSize());
        System.out.println(executor.getTaskCount());
    }
}
